package math

import (
	"strconv"
	"github.com/shopspring/decimal"
)

func MultiTotalPriceInt(price float32, quantity int32) int64 {
    priceStr := strconv.FormatFloat(float64(price), 'f', -1, 32)  //不能填写64，否则也会不准
    pricePoint, _:= decimal.NewFromString(priceStr)
    quantityPoint := decimal.New(int64(quantity), 0)
    result := pricePoint.Mul(quantityPoint).Round(2)
	return result.IntPart()
}

func MultiTotalPrice(price float32, quantity int32) float32 {
    priceStr := strconv.FormatFloat(float64(price), 'f', -1, 32)  //不能填写64，否则也会不准
    pricePoint, _:= decimal.NewFromString(priceStr)
    quantityPoint := decimal.New(int64(quantity), 0)
    result := pricePoint.Mul(quantityPoint).Round(2)
    value, _ := result.Float64()
	return float32(value)
}

func MultiFloat32Price(multi1 float32, multi2 float32) float32 {
    multi1Str := strconv.FormatFloat(float64(multi1), 'f', -1, 32)  //不能填写64，否则也会不准
    multi1Point, _:= decimal.NewFromString(multi1Str)
    //
	multi2Str := strconv.FormatFloat(float64(multi2), 'f', -1, 32)  //不能填写64，否则也会不准
    multi2Point, _:= decimal.NewFromString(multi2Str)
	//
	result := multi1Point.Mul(multi2Point).Round(2)
    value, _ := result.Float64()
	return float32(value)
}

func SubPrice(srcValue, subValue float32) float32 {
    srcValueStr := strconv.FormatFloat(float64(srcValue), 'f', -1, 32)  //不能填写64，否则也会不准
    srcValuePoint, _:= decimal.NewFromString(srcValueStr)
	subValueStr := strconv.FormatFloat(float64(subValue), 'f', -1, 32)  //不能填写64，否则也会不准
    subValuePoint, _:= decimal.NewFromString(subValueStr)
	result := srcValuePoint.Sub(subValuePoint).Round(2)
    value, _ := result.Float64()
	return float32(value)
}

func CeilPostFee(quantity, basePiece, price float32) float32 {
	quantityStr := strconv.FormatFloat(float64(quantity), 'f', -1, 32)
	quantityPoint, _:= decimal.NewFromString(quantityStr)
	basePieceStr := strconv.FormatFloat(float64(basePiece), 'f', -1, 32)
	basePiecePoint, _:= decimal.NewFromString(basePieceStr)
	divResult := quantityPoint.Div(basePiecePoint)
    ceilResult := divResult.Ceil()
	//
	priceStr := strconv.FormatFloat(float64(price), 'f', -1, 32)  //不能填写64，否则也会不准
    pricePoint, _:= decimal.NewFromString(priceStr)
	result := pricePoint.Mul(ceilResult).Round(2)
	value, _ := result.Float64()
	return float32(value)
}

func DivTotalFee(totalFee float32, quantity int) float32 {
	totalFeeStr := strconv.FormatFloat(float64(totalFee), 'f', -1, 32)  //不能填写64，否则也会不准
    feePoint, _:= decimal.NewFromString(totalFeeStr)
	quantityPoint := decimal.New(int64(quantity), 0)
	result := feePoint.Div(quantityPoint).Round(2)
	value, _ := result.Float64()
	return float32(value)
}

func DivTotalFee2(totalFee float32, quantity int) float32 {
	totalFeeStr := strconv.FormatFloat(float64(totalFee), 'f', -1, 32)  //不能填写64，否则也会不准
    feePoint, _:= decimal.NewFromString(totalFeeStr)
	quantityPoint := decimal.New(int64(quantity), 0)
	result := feePoint.Div(quantityPoint).Round(4)
	value, _ := result.Float64()
	return float32(value)
}
